package com.example.dps;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import com.downpayment.dps.R;


public class ResourcesActivity extends Activity {
	
	Map<String, String> terms = new HashMap<String, String>();
	
	String vocabText = "";
	
	LinearLayout mainContentLayout;
	
	WebView webView;
	
	
	Handler handler = new Handler();
	
	public static final String URL_SIXTHINGSTOKNOW = "sixthingstoknow.html";
	public static final String URL_FHAMORTGAGES = "fhamortgages.html";
	public static final String URL_FINDMORTGAGECOUNCILOR = "findmortgagecouncilor.html";
	public static final String URL_HUDPROGRAMS = "otherHudPrograms.html";
	public static final String URL_PREAPPROVE = "mortgagePreApproval.html";
	public static final String URL_TENSTEPSTOBUYINGHOME = "tenStepsToBuyingAHouse.html";
	
	
	
	
	public void loadWebView(){
		WebView webView = (WebView) findViewById(R.id.viewResourcesWebView);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.loadUrl("file:///android_asset/sixthingstoknow.html");
		
		webView.addJavascriptInterface(new WebAppInterface(this), "Android");
		
		addBackButtonClickListener();
	}
	
	@Override
	public void onBackPressed(){
		WebView webView = (WebView) findViewById(R.id.viewResourcesWebView);
		
		if(webView != null){
			ImageButton backButton = (ImageButton) findViewById(R.id.backButton);
			
			ResourcesActivity.this.changeContent(R.layout.resources_main_menu, 0);
			
			backButton.setVisibility(View.GONE);
			
			ResourcesActivity.this.addResourcesMenuOnClickListeners();
		}
		else{
			this.finish();
		}
		
		
	}
	
	@Override
	public void onResume(){
		super.onResume();

		
		ScrollView sv = (ScrollView) this.findViewById(R.id.scrollView);
		if(sv!=null) sv.scrollTo(0,0);
		
		Intent intent = getIntent();
		
		String viewResource = null;
		
		if(intent != null && intent.getExtras() != null){
			viewResource = intent.getExtras().getString("viewResource");
		}
		else{

		}

		if(viewResource != null){
			if(viewResource.equals("mortgagePreApp")){
				this.loadHtmlAsset(URL_PREAPPROVE);
			}
		}
	}
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.resources_layout);
		
		
		LayoutInflater inflater = ResourcesActivity.this.getLayoutInflater();
		
		this.mainContentLayout = (LinearLayout) findViewById(R.id.resourcesMainContent);
		
		mainContentLayout.removeAllViews();
		
		mainContentLayout.addView(inflater.inflate(R.layout.resources_main_menu, mainContentLayout, false));
		
		
		addResourcesMenuOnClickListeners();
		
		initTerms();
		
		
		
		
	}
	

	
	@Override
	public Dialog onCreateDialog(int id) {
		
		AlertDialog.Builder builder = null;
		LayoutInflater inflater = null;
		
		switch(id){
			case Constants.PRIVACY_POLICY_DIALOG:
				builder = new AlertDialog.Builder(this);
			    // Get the layout inflater
			    inflater = this.getLayoutInflater();

			    // Inflate and set the layout for the dialog
			    // Pass null as the parent view because its going in the dialog layout
			    builder.setView(inflater.inflate(R.layout.privacy_policy_layout, null))
			    // Add action buttons
			           .setPositiveButton("Continue", new DialogInterface.OnClickListener() {
			               @Override
			               public void onClick(DialogInterface dialog, int id) {
			            	   
			            	   
			               }
			           });
   
			    
			    AlertDialog dialog = builder.create();

			    return dialog;
				
				
			default:
			
				builder = new AlertDialog.Builder(this);
				builder.setMessage(this.vocabText + ":\n\n" + this.terms.get(this.vocabText.toLowerCase()))
		    		.setNegativeButton("Close", null);
				return builder.create();
		}
		
		
	}
	
	
	public class WebAppInterface {
	    Activity activity;
	    
	    int index = 0;

	    /** Instantiate the interface and set the context */
	    WebAppInterface(Activity a) {
	        this.activity = a;
	    }

	    
	    @JavascriptInterface
	    public void showDefinition(String word) {

	    	ResourcesActivity.this.vocabText=word;
	    	
	        ResourcesActivity.this.vocabText = word;
	    	
	        this.activity.showDialog(index);
	        
	        index++;
	        
	    }
	    
	    @JavascriptInterface
	    public void showToast(String toast) {
	        Toast.makeText(ResourcesActivity.this, toast, Toast.LENGTH_SHORT).show();
	    }
	    
	    public void showPrivacyPolicy(){
	    		    	showDialog(Constants.PRIVACY_POLICY_DIALOG);
	    	
	    }
	}

	

	
	public void addBackButtonClickListener(){
		
		
		
		final ImageButton backButton = (ImageButton) findViewById(R.id.backButton);
		
		backButton.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				ResourcesActivity.this.changeContent(R.layout.resources_main_menu, 0);
				
				backButton.setVisibility(View.GONE);
				
				ResourcesActivity.this.addResourcesMenuOnClickListeners();
				
			}
			
		});
		
		backButton.setOnTouchListener(new OnTouchListener(){

			@Override
			public boolean onTouch(View view, MotionEvent event) {
				// TODO Auto-generated method stub
				
				ImageView button = (ImageView) view;
				
				if(event.getAction() == MotionEvent.ACTION_DOWN){
					button.setImageDrawable(view.getResources().getDrawable(R.drawable.back_btn_clicked));
				}
				else if(event.getAction() == MotionEvent.ACTION_UP){
					button.setImageDrawable(view.getResources().getDrawable(R.drawable.back_btn));
				}
				else if(event.getAction() == MotionEvent.ACTION_CANCEL){
					button.setImageDrawable(view.getResources().getDrawable(R.drawable.back_btn));
				}
				
				
				return false;
			}
			
		});
		
		backButton.setVisibility(View.VISIBLE);
		
		
		
	}
	
	
	
	public void addResourcesMenuOnClickListeners(){

		LinearLayout sixThingsButtonLayout = (LinearLayout) findViewById(R.id.sixThingsButtonLayout);
		sixThingsButtonLayout.setBackgroundDrawable(null);
		
		sixThingsButtonLayout.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				/** start tabbed view resource activity **/
				ResourcesActivity.this.loadHtmlAsset(URL_SIXTHINGSTOKNOW);
			}

		});
		
		sixThingsButtonLayout.setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				/** get the root element of the button view **/
		    	LinearLayout view = (LinearLayout) v;
		    	
		        if(event.getAction() == MotionEvent.ACTION_DOWN) {
		        	/** change the image button picture when clicked **/
		        	view.setBackgroundDrawable(v.getResources().getDrawable(R.drawable.resources_menu_item_background_nine));
		        } else if (event.getAction() == MotionEvent.ACTION_UP) {
		        	view.setBackgroundDrawable(null);
		        }
		        else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
		        	view.setBackgroundDrawable(null);
		        }
				return false;
			}
		});
		
		
		LinearLayout fhaMortgageButtonLayout = (LinearLayout) findViewById(R.id.fhaMortgageLayout);
		
		fhaMortgageButtonLayout.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				/** start tabbed view resource activity **/
				ResourcesActivity.this.loadHtmlAsset(URL_FHAMORTGAGES);
			}

		});
		
		fhaMortgageButtonLayout.setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				/** get the root element of the button view **/
		    	LinearLayout view = (LinearLayout) v;
		    	
		        if(event.getAction() == MotionEvent.ACTION_DOWN) {
		        	/** change the image button picture when clicked **/
		        	view.setBackgroundDrawable(v.getResources().getDrawable(R.drawable.resources_menu_item_background_nine));
		        } else if (event.getAction() == MotionEvent.ACTION_UP) {
		        	view.setBackgroundDrawable(null);
		        }
		        else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
		        	view.setBackgroundDrawable(null);
		        }
				return false;
			}
		});
		
		LinearLayout tenStepsToBuyingHomeButtonLayout = (LinearLayout) findViewById(R.id.tenStepsToBuyingHomeButtonLayout);
		
		tenStepsToBuyingHomeButtonLayout.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				/** start tabbed view resource activity **/
				ResourcesActivity.this.loadHtmlAsset(URL_TENSTEPSTOBUYINGHOME);
			}

		});
		
		tenStepsToBuyingHomeButtonLayout.setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				/** get the root element of the button view **/
		    	LinearLayout view = (LinearLayout) v;
		    	
		        if(event.getAction() == MotionEvent.ACTION_DOWN) {
		        	/** change the image button picture when clicked **/
		        	view.setBackgroundDrawable(v.getResources().getDrawable(R.drawable.resources_menu_item_background_nine));
		        } else if (event.getAction() == MotionEvent.ACTION_UP) {
		        	view.setBackgroundDrawable(null);
		        }
		        else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
		        	view.setBackgroundDrawable(null);
		        }
				return false;
			}
		});
		
		
		LinearLayout findMortgageCouncilorButtonLayout = (LinearLayout) findViewById(R.id.findMortgageCouncilorButtonLayout);
		
		findMortgageCouncilorButtonLayout.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				/** start tabbed view resource activity **/
				ResourcesActivity.this.loadHtmlAsset(URL_FINDMORTGAGECOUNCILOR);
			}

		});
		
		findMortgageCouncilorButtonLayout.setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				/** get the root element of the button view **/
		    	LinearLayout view = (LinearLayout) v;
		    	
		        if(event.getAction() == MotionEvent.ACTION_DOWN) {
		        	/** change the image button picture when clicked **/
		        	view.setBackgroundDrawable(v.getResources().getDrawable(R.drawable.resources_menu_item_background_nine));
		        } else if (event.getAction() == MotionEvent.ACTION_UP) {
		        	view.setBackgroundDrawable(null);
		        }
		        else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
		        	view.setBackgroundDrawable(null);
		        }
				return false;
			}
		});
		
		
		LinearLayout hudProgramsButtonLayout = (LinearLayout) findViewById(R.id.hudProgramsButtonLayout);
		
		hudProgramsButtonLayout.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				/** start tabbed view resource activity **/
				ResourcesActivity.this.loadHtmlAsset(URL_HUDPROGRAMS);
			}

		});
		
		hudProgramsButtonLayout.setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				/** get the root element of the button view **/
		    	LinearLayout view = (LinearLayout) v;
		    	
		        if(event.getAction() == MotionEvent.ACTION_DOWN) {
		        	/** change the image button picture when clicked **/
		        	view.setBackgroundDrawable(v.getResources().getDrawable(R.drawable.resources_menu_item_background_nine));
		        } else if (event.getAction() == MotionEvent.ACTION_UP) {
		        	view.setBackgroundDrawable(null);
		        }
		        else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
		        	view.setBackgroundDrawable(null);
		        }
				return false;
			}
		});
		
		
		
		LinearLayout preapproveButtonLayout = (LinearLayout) findViewById(R.id.preapproveButtonLayout);
		
		preapproveButtonLayout.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View arg0) {
				/** start tabbed view resource activity **/
				ResourcesActivity.this.loadHtmlAsset(URL_PREAPPROVE);
			}

		});
		
		preapproveButtonLayout.setOnTouchListener(new OnTouchListener(){
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				/** get the root element of the button view **/
		    	LinearLayout view = (LinearLayout) v;
		    	
		        if(event.getAction() == MotionEvent.ACTION_DOWN) {
		        	/** change the image button picture when clicked **/
		        	view.setBackgroundDrawable(v.getResources().getDrawable(R.drawable.resources_menu_item_background_nine));
		        } else if (event.getAction() == MotionEvent.ACTION_UP) {
		        	view.setBackgroundDrawable(null);
		        }
		        else if (event.getAction() == MotionEvent.ACTION_CANCEL) {
		        	view.setBackgroundDrawable(null);
		        }
				return false;
			}
		});
		
	}
	
	private void changeContent(int layoutID, int resourceID) {
		// TODO Auto-generated method stub
		
		LayoutInflater inflater = ResourcesActivity.this.getLayoutInflater();
		
		LinearLayout mainContentLayout = (LinearLayout) findViewById(R.id.resourcesMainContent);
		
		mainContentLayout.removeAllViews();
		
		mainContentLayout.addView(inflater.inflate(layoutID, mainContentLayout, false));
		
		
		/** if viewing a resource, set the resource text to the resource string at resource ID **/
		if(layoutID == R.layout.view_resource_layout && resourceID != 0){
			
			TextView contentText = (TextView) findViewById(R.id.viewResourceText);
			
			contentText.setText(getResources().getString(resourceID));
			
			
		}

		
		
		
		if(layoutID == R.layout.view_resources_web_layout){
			
			webView = (WebView) findViewById(R.id.viewResourcesWebView);
			
			webView.getSettings().setJavaScriptEnabled(true);
			webView.loadUrl("file:///android_asset/sixthingstoknow.html");
			webView.addJavascriptInterface(new WebAppInterface(ResourcesActivity.this), "Android");
			
			addBackButtonClickListener();

			
		}
		
		else if (layoutID == R.layout.resources_main_menu){
			
			addResourcesMenuOnClickListeners();
			
		}

	}
	
	public void initTerms(){
		
		String[] vocabWords = new String[] { "Principal", "Interest", "Taxes", "insurance", "Fees", "Closing Costs" };
		String[] vocabDefs = new String[] {
				"The amount you owe on your loan, not including interest",

				"The interest payment on the remaining principal",

				"Includes property taxes, municipal, school taxes, etc.",

				"Homeowners insurance and mortgage insurance",

				"May include homeowners association, condo fees, etc.",
				
				"Including various processing and filing fees, title insurance, various taxes, attorney fees, commissions, insurance, and other amounts due at settlement."
		
		};
		
		

		for(int i = 0; i < vocabWords.length; i++){
			this.terms.put(vocabWords[i].toLowerCase(), vocabDefs[i]);
		}
		
	}
	
	public void loadHtmlAsset(String url){
		
		LayoutInflater inflater = ResourcesActivity.this.getLayoutInflater();

		int layoutID = R.layout.view_resources_web_layout;
		
		LinearLayout mainContentLayout = (LinearLayout) findViewById(R.id.resourcesMainContent);
		
		mainContentLayout.removeAllViews();
		
		mainContentLayout.addView(inflater.inflate(layoutID, mainContentLayout, false));
		

		
		if(layoutID == R.layout.view_resources_web_layout){
			
			webView = (WebView) findViewById(R.id.viewResourcesWebView);
			
			webView.getSettings().setJavaScriptEnabled(true);
			webView.loadUrl("file:///android_asset/" + url);
			webView.addJavascriptInterface(new WebAppInterface(ResourcesActivity.this), "Android");
			
			addBackButtonClickListener();

			
		}
		
		else if (layoutID == R.layout.resources_main_menu){
			
			addResourcesMenuOnClickListeners();
			
		}
		
	}
	
}
